-- Radical by Basil Voronkov
-- Module 'mainmenu'
---------------------------------------------
-- Implements main menu screens
require "lib/core"
require "lib/const"
require "lib/drawing"
require "lib/engine"

mainmenu = {
  x = 660,
  y = 250,
  itemcolor = const.linkcolor,
  itemfont = "sans-b.ttf",
  itemfontsize = 18,
  image = const.path.gfx.."main.jpg"
}

function mainmenu.init()
  _menuback = nil;
  menu_object = mainmenu.menuitem();
  take(menu_object);
  sys_menuroom = mainmenu.titlescreen {
     {"resume", "Продолжить игру", mainmenu.resume, mainmenu.canresume, 20}
    ,{"new", "Новая игра", gamefile_("4-mad/crush.lua") }
    ,{"save", "Сохранить", exec_("stead.menu_toggle('save')")}
    ,{"load", "Загрузить", exec_("stead.menu_toggle('load')")}
    ,{"ep", "Выбор эпизода", mainmenu.walkscr_("sys_episodes")}
    ,{"achiv", "Достижения", mainmenu.walkscr_("sys_bonuses")}
    ,{"exit", "Выход", exec_("stead.menu_toggle('exit')")}
  }
end

function mainmenu.walkscr_(o)
  return function()
    play_sound("flash");
    walkblack_(o)();
  end
end

function mainmenu.canresume()
  return _menuback ~= nil or prefs["last_episode"] ~= nil;
end

function mainmenu.resume()
  restore_music();
  if _menuback ~= nil then
    local mb = _menuback;
    _menuback = nil;
    walk(mb);
  elseif prefs["last_episode"] ~= nil then
    if prefs["last_episode"] == "$checkpoint$" then
      load_checkpoint();
    else
      gamefile(const.path.game..prefs["last_episode"], true);
    end
  end
end

function mainmenu.open()
  local hw = here();
  if not hw.hidemenu then
    _menuback = deref(hw);
    save_music();
    stop_music();
    walk("sys_menuroom");
  end
end

function mainmenu.menuitem()
  return menu {
     nam        = function(s)
                    local hw = here();
                    if not hw.hidemenu then
                      return img("theme/gamemenu.png")
                    end
                  end
    ,menu       = mainmenu.open
  }
end

function mainmenu.titlescreen(obj)
  local ret = engine.gameroom {
     tile       = mainmenu.image
    ,hidemenu   = true
    ,fps        = 20
    ,ondrawbg   = function(s,p)
                    local x,y = mainmenu.x,mainmenu.y;
                    if s.map == nil then
                      s.map = map(0, 0, const.picwidth, const.picheight);
                    end
                    s.hash = {};
                    for i,v in pairs(obj) do
                      local col = mainmenu.itemcolor;
                      if v[4] ~= nil and not v[4]() then
                        col = const.bordercolor;
                      end
                      local spr = sprite.text(drawing.font(mainmenu.itemfont, mainmenu.itemfontsize), v[2], col);
                      local w,h = sprite.size(spr);
                      sprite.draw(spr, p, x, y);
                      sprite.free(spr);
                      if col ~= const.bordercolor then
                        s.hash[v[1]] = v[3];
                        s.map:add(v[1], x, y, w, h); 
                      end
                      y = y + 40;
                      if v[5] ~= nil then
                        y = y + v[5];
                      end
                    end
                    local spr = sprite.text(drawing.font(const.font, 10), "Версия "..gameversion, const.textcolor);
                    local sw,sh = sprite.size(spr);
                    sprite.draw(spr, p, const.winwidth - sw - 5, const.winheight - sh - 2);
                    sprite.free(spr);
                    return p;
                  end
    ,onclick    = function(s, x, y)
                    local sel = s.map:get(x, y);
                    if sel ~= nil then
                      local o = s.hash[sel];
                      o();
                    end
                  end
  }
  ret.kbd       = function(s)
                    mainmenu.resume();
                    return true;
                  end
  return ret;
end
